package org.neuedu.servlet;

import org.neuedu.bean.Reply;
import org.neuedu.bean.User;
import org.neuedu.service.ReplyService;
import org.neuedu.service.ReplyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ArticleReplyServlet",urlPatterns = "/article/reply")
public class ArticleReplyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取登录者信息
        User fly_user = (User) request.getSession().getAttribute("fly_user");
        if (fly_user == null) {
            response.sendRedirect(request.getContextPath()+"/forward/login");
        }else{
            String content = request.getParameter("content");
            String aid = request.getParameter("aid");
            Integer id = fly_user.getId();
            // 封装
            Reply reply = new Reply();
            reply.setUid(id);
            reply.setAid(Integer.valueOf(aid));
            reply.setReplyContent(content);
            // 调service
            ReplyService service = new ReplyServiceImpl();
            int i = service.reply(reply);
            if(i != 0){
                // success
                response.sendRedirect(request.getContextPath()+"/article/detail?aid="+aid);
            }else{
                request.setAttribute("replyFailMsg","评论失败");
                request.getRequestDispatcher("/article/detail?aid="+aid).forward(request,response);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
